(function($) {
    var arkflow = $.arkflow;

    // 创建路径对象
    arkflow.PathLinePoints = Base.extend({
        constructor : function(path) {
            this.path = path;
            this.fromNode = path.fromNode;
            this.toNode = path.toNode;
            this.paper = path.paper;
            this.pathConfig = path.pathConfig;

            var fromBBox = this.fromNode.getBBox();
            var toBBox = this.toNode.getBBox();

            // 两节点中心点连线与from节点的交点
            var fromConnPoint = arkflow.util.connPoint(fromBBox, {
                x : toBBox.x + toBBox.width / 2,
                y : toBBox.y + toBBox.height / 2
            });

            // 两节点中心点连线与to节点的交点
            var toConnPoint = arkflow.util.connPoint(toBBox, fromConnPoint);
            //      两交点的中心点
            var smallDot = new arkflow.PathPoint("small", {
                x : (fromConnPoint.x + toConnPoint.x) / 2,
                y : (fromConnPoint.y + toConnPoint.y) / 2
            }, null, null, path);
            this._fromDot = new arkflow.PathPoint("from", fromConnPoint, null, smallDot, path);
            this._fromDot.right().left(this._fromDot);

            this._toDot = new arkflow.PathPoint("to", toConnPoint, this._fromDot.right(), null, path);
            this._fromDot.right().right(this._toDot);

            this.bindPathClick();
            this.hide();
        },
        bindPathClick : function() {
            var me = this;
            // 点击连线，显示属性和周围选择提示框
            var pathClick = function(e, comp) {
                if (!arkflow.config.editable) {
                    return;
                }
                if (comp && comp.getId() == me.path.id) {
                    me.show();
                    $(me.paper).trigger("showprops", [me.pathConfig.props, me.path]);
                } else {
                    me.hide();
                }
                var o = $(me.paper).data("mod");
                switch (o) {
                    case "pointer" :
                        break;
                    case "path" :
                        break;
                }
            };
            $(this.paper).bind("click", pathClick);
        },
        lastPoint:function() {
            var fromDotTemp = this._fromDot;
            while (fromDotTemp.right()) {
                fromDotTemp = fromDotTemp.right();
            }
            return fromDotTemp;
        },
        toPathString : function() {
            if (!this._fromDot) {
                return "";
            }
            var fromDotTemp = this._fromDot;
            var pashStr = "M" + fromDotTemp.pos().x + " " + fromDotTemp.pos().y;

            while (fromDotTemp.right()) {
                fromDotTemp = fromDotTemp.right();
                pashStr += "L" + fromDotTemp.pos().x + " " + fromDotTemp.pos().y;
            }


            return [pashStr];
        },
        // json字符串拼装
        toJson : function() {
            var result = "[";
            var fromDot = this._fromDot;
            while (fromDot) {
                if (fromDot.type() == "big") {
                    result += "{x:" + Math.round(fromDot.pos().x) + ",y:" + Math.round(fromDot.pos().y) + "},";
                }
                fromDot = fromDot.right();
            }
            if (result.substring(result.length - 1, result.length) == ",") {
                result = result.substring(0, result.length - 1);
            }
            result += "]";
            return result;
        },
        restore : function(obj) {
            var rightDot = this._fromDot.right();
            for (var i = 0; i < obj.length; i++) {
                rightDot.moveTo(obj[i].x, obj[i].y);
                rightDot.moveTo(obj[i].x, obj[i].y);
                rightDot = rightDot.right();
            }
            this.hide();
        },
        fromDot : function() {
            return this._fromDot;
        },
        hasPoint:function(){
            return !!this._fromDot;
        },
        toDot : function() {
            return this._toDot;
        },
        midDot : function() {
            var midDot = this._fromDot.right();
            var rrDot = this._fromDot.right().right();
            while (rrDot.right() && rrDot.right().right()) {
                rrDot = rrDot.right().right();
                midDot = midDot.right();
            }
            return midDot;
        },
        show : function() {
            var dot = this._fromDot;
            while (dot) {
                dot.node().show();
                dot = dot.right();
            }
        },
        hide : function() {
            var dot = this._fromDot;
            while (dot) {
                dot.node().hide();
                dot = dot.right();
            }
        },
        remove : function() {
            var dot = this._fromDot;
            while (dot) {
                if (dot.right()) {
                    dot = dot.right();
                    dot.left().remove();
                } else {
                    dot.remove();
                    dot = null;
                }
            }
        }
    });
})(jQuery);
